/**************************************************************************

	Copyright (c) 2001-2003 Geir Landr� (drop@destroydrop.com)
	JavaScript Tree - www.destroydrop.com/hjavascripts/tree/
	Version 0.96	

	This script can be used freely as long as all copyright messages are
	intact.
**************************************************************************/
/**
	<script type="text/javascript">
		
		var Tree = new Array;
		// nodeId | parentNodeId | nodeName | nodeUrl
		Tree[0]  = "1|0|Page 1|#";
		Tree[1]	 = "2|1|Page 1.1|#";
		Tree[2]  = "3|1|Page 1.2|#";
		Tree[3]  = "4|3|Page 1.2.1|#";
		Tree[4]  = "5|1|Page 1.3|#";
		Tree[5]  = "6|2|Page 1.1.1|#";
		Tree[6]  = "7|6|Page 1.1.1.1|#";
		Tree[7]  = "8|6|Page 1.1.1.2|#";
		Tree[8]  = "9|1|Page 1.4|#";
		Tree[9]  = "10|9|Page 1.4.1|#";
		Tree[10] = "11|0|Page 2|#";
	
	</script>

<div class="tree">
<script type="text/javascript">
	
	createTree(Tree);

</script>
*/

function nota(id) {
	if(seleccionados.length > 0) {
		var href = document.getElementById(id).href;
		var tmp = new Array();
		for(var i = 0; i < seleccionados.length; i++) {
			tmp.push(seleccionados[i].split(":").pop());
		}
		href = href + "/multiples:" + tmp.join('*');
		document.getElementById(id).href = href;
	}
	var msg = 'Para ver este archivo en excel deberá decargar desde el botón "modelo.xls" que se encuentra en esta misma pagina el archivo modelo.xls.';
	msg += '\nUna vez guardado el archivo modelo.xls habralo con MS Excel y presione la tecla control simultaneamente con la tecla i, luego siga las intrucciones.';
	alert(msg);
}

// Arrays for nodes and icons
var nodes			= new Array();;
var openNodes	= new Array();
var icons			= new Array(6);
var path;
var archivo;

// Loads all icons that are used in the tree
function preloadIcons() {
	icons[0] = new Image();
	icons[0].src = path + "plus.gif";
	icons[1] = new Image();
	icons[1].src = path + "plusbottom.gif";
	icons[2] = new Image();
	icons[2].src = path + "minus.gif";
	icons[3] = new Image();
	icons[3].src = path + "minusbottom.gif";
	icons[4] = new Image();
	icons[4].src = path + "folder.gif";
	icons[5] = new Image();
	icons[5].src = path + "folderopen.gif";
}
// Create the tree
function createTree(arrName, startNode, openNode, ppath, a) {
	archivo = a;
	path = ppath;
	nodes = arrName;
	if (nodes.length > 0) {
		preloadIcons();
		if (startNode == null) startNode = 0;
		if (openNode != 0 || openNode != null) setOpenNodes(openNode);
	
		if (startNode !=0) {
			var nodeValues = nodes[getArrayId(startNode)].split("|");
			document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\"><img src=\"" + path + "folderopen.gif\" align=\"absbottom\" alt=\"\" />" + nodeValues[2] + "</a><br />");
		} else document.write("<img src=\"" + path + "base.gif\" align=\"absbottom\" alt=\"\" />Datagro<br />");
	
		var recursedNodes = new Array();
		addNode(startNode, recursedNodes);
	}
}
// Returns the position of a node in the array
function getArrayId(node) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==node) return i;
	}
}
// Puts in array nodes that will be open
function setOpenNodes(openNode) {
	for (i=0; i<nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[0]==openNode) {
			openNodes.push(nodeValues[0]);
			setOpenNodes(nodeValues[1]);
		}
	} 
}
// Checks if a node is open
function isNodeOpen(node) {
	for (i=0; i<openNodes.length; i++)
		if (openNodes[i]==node) return true;
	return false;
}
// Checks if a node has any children
function hasChildNode(parentNode) {
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) return true;
	}
	return false;
}
// Checks if a node is the last sibling
function lastSibling (node, parentNode) {
	var lastChild = 0;
	for (i=0; i< nodes.length; i++) {
		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode)
			lastChild = nodeValues[0];
	}
	if (lastChild==node) return true;
	return false;
}

Math.randomMax = function(maxVal,asFloat){
	var val = Math.random()*maxVal;
	return asFloat?val:Math.round(val);
}

// Adds a new node to the tree
function addNode(parentNode, recursedNodes) {
	for (var i = 0; i < nodes.length; i++) {

		var nodeValues = nodes[i].split("|");
		if (nodeValues[1] == parentNode) {
			
			var ls	= lastSibling(nodeValues[0], nodeValues[1]);
			var hcn	= hasChildNode(nodeValues[0]);
			var ino = isNodeOpen(nodeValues[0]);

			// Write out line & empty icons
			for (g=0; g<recursedNodes.length; g++) {
				if (recursedNodes[g] == 1) document.write("<img src=\"" + path + "line.gif\" align=\"absbottom\" alt=\"\" />");
				else  document.write("<img src=\"" + path + "empty.gif\" align=\"absbottom\" alt=\"\" />");
			}

			// put in array line & empty icons
			if (ls) recursedNodes.push(0);
			else recursedNodes.push(1);

			// Write out join icons
			if (hcn) {
				if (ls) {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 1);\"><img id=\"join" + nodeValues[0] + "\" src=\"" + path + "");
					 	if (ino) document.write("minus");
						else document.write("plus");
					document.write("bottom.gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				} else {
					document.write("<a href=\"javascript: oc(" + nodeValues[0] + ", 0);\"><img id=\"join" + nodeValues[0] + "\" src=\"" + path + "");
						if (ino) document.write("minus");
						else document.write("plus");
					document.write(".gif\" align=\"absbottom\" alt=\"Open/Close node\" /></a>");
				}
			} else {
				if (ls) document.write("<img src=\"" + path + "joinbottom.gif\" align=\"absbottom\" alt=\"\" />");
				else document.write("<img src=\"" + path + "join.gif\" align=\"absbottom\" alt=\"\" />");
			}
			
			// Write out folder & page icons
			if (hcn) {
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"" + path + "folder")
					if (ino) document.write("open");
				document.write(".gif\" align=\"absbottom\" alt=\"Folder\" />");
			} else {
				document.write("<img id=\"icon" + nodeValues[0] + "\" src=\"" + path + "page.gif\" align=\"absbottom\" alt=\"Page\" onclick=\"seleccionar(this, '" + nodeValues[3] + "', '" + nodeValues[1] + "')\"/>");
			}
			

			// Start link
			//document.write("<a href=\"" + nodeValues[3] + "\" onmouseover=\"window.status='" + nodeValues[2] + "';return true;\" onmouseout=\"window.status=' ';return true;\">");
			if (hcn)
				document.write("<a href=\"" + nodeValues[3] + "\">");
			else {
				var theNumber = Math.randomMax( 100000 );
				if(archivo) {
					document.write("<a id='nota_" + theNumber + "' onclick=\"nota(\'nota_" + theNumber + "\');\" href=\"" + nodeValues[3] + "\">");
				}
				else {
					//document.write("<a id='mostrar_mapa_" + theNumber + "' onclick='return false;' href=\"" + nodeValues[3] + "\">");
					document.write("<a id='mostrar_mapa_" + theNumber + "' onclick='getX(this.id);' href='" + nodeValues[3] + "'>");
					/*
					Event.observe("mostrar_mapa_"+theNumber, "click",
						function(event) {
								document.getElementById("link").value = document.getElementById("mostrar_mapa_"+theNumber).href;
								var f = asignarFiltrosX("mostrar_mapa_"+theNumber);
								new Ajax.Updater("map", f,
								{asynchronous:true,
								evalScripts:true,
								requestHeaders:["X-Update", "map"]})
							}, false);
					*/
				}
			}
			
			
			// Write out node name
			document.write(nodeValues[2]);

			// End link
			document.write("</a><br />");
			
			// If node has children write out divs and go deeper
			if (hcn) {
				document.write("<div id=\"div" + nodeValues[0] + "\"");
					if (!ino) document.write(" style=\"display: none;\"");
				document.write(">");
				addNode(nodeValues[0], recursedNodes);
				document.write("</div>");
			}
			
			// remove last line or empty icon 
			recursedNodes.pop();
		}
	}
}

		
		var padreSeleccionado = null;
		var seleccionados = new Array();
		function seleccionar (img, href, padre){
			
			if(padreSeleccionado == null) {
				padreSeleccionado = padre;
			}
			else if(padreSeleccionado != padre) {
				alert('Solo puede seleccionar archivos del mismo campo, lote y cultivo.');
				return;
			}
			if(img.src.indexOf("ok.png") > 0) {
				img.src = path + "page.gif";
				var seleccionadosTmp = new Array();
				for(var i = 0; i < seleccionados.length; i++) {
					if(seleccionados[i] != href) {
						seleccionadosTmp.push(seleccionados[i]);	
					}
				}
				seleccionados = seleccionadosTmp;
				if(seleccionados.length == 0) {
					padreSeleccionado = null;
				}
			}
			else {
				img.src = path + "ok.png";
				seleccionados.push(href);
			}
		}
		
		function getX (id) {
				document.getElementById("link").value = document.getElementById(id).href;
				var f = asignarFiltrosX(id);
				document.getElementById(id).href = "javascript:void(0)";
				new Ajax.Updater("map", f,
					{asynchronous:true,
					evalScripts:true,
					requestHeaders:["X-Update", "map"]});
				return false;
			};
		
		function asignarFiltrosX (id){

			var href = document.getElementById(id);
			var parametro = document.getElementById("parametro");
			href = href + "/parametro:" + parametro.value;

			parametro = document.getElementById("precision");
			href = href + "/precision:" + parametro.value;
			
			if(seleccionados.length > 0) {
				var tmp = new Array();
				for(var i = 0; i < seleccionados.length; i++) {
					tmp.push(seleccionados[i].split(":").pop());
				}
				href = href + "/multiples:" + tmp.join('*');
			}

			return href;
		}



// Opens or closes a node
function oc(node, bottom) {
	var theDiv = document.getElementById("div" + node);
	var theJoin	= document.getElementById("join" + node);
	var theIcon = document.getElementById("icon" + node);
	
	if (theDiv.style.display == 'none') {
		if (bottom==1) theJoin.src = icons[3].src;
		else theJoin.src = icons[2].src;
		theIcon.src = icons[5].src;
		theDiv.style.display = '';
	} else {
		if (bottom==1) theJoin.src = icons[1].src;
		else theJoin.src = icons[0].src;
		theIcon.src = icons[4].src;
		theDiv.style.display = 'none';
	}
}
// Push and pop not implemented in IE
if(!Array.prototype.push) {
	function array_push() {
		for(var i=0;i<arguments.length;i++)
			this[this.length]=arguments[i];
		return this.length;
	}
	Array.prototype.push = array_push;
}
if(!Array.prototype.pop) {
	function array_pop(){
		lastElement = this[this.length-1];
		this.length = Math.max(this.length-1,0);
		return lastElement;
	}
	Array.prototype.pop = array_pop;
}
